package com.aplink.generic.sharepreference;

import android.content.Context;
import android.preference.PreferenceManager;

import com.aplink.generic.GenericApplication;
import com.aplink.generic.cache.MasterConfigManager;
import com.aplink.generic.constant.type.SharePreferenceType;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.util.Language;

public class GenericSharePreference {
	private static final String PREF_APP_VERSION = "APP_VERSION";
	private static final String PREF_APPLICATION_NAME = "APPLICATION_NAME";
	private static final String PREF_AUTO_SYNC_PENDING = "PREF_AUTO_SYNC_PENDING";
	private static final String PREF_CLOUD_MESSAGE = "PREF_CLOUD_MESSAGE";
	private static final String PREF_CURRENT_LANGUAGE = "CURRENT_LANGUAGE";
	private static final String PREF_DATABASE_MODIFIED = "PREF_DATABASE_MODIFIED";
	private static final String PREF_ENABLE_AUTO_SYNC = "PREF_ENABLE_AUTO_SYNC";
	private static final String PREF_FIRST_START = "PREF_FIRST_START";
	private static final String PREF_INVALID_TOKEN = "INVALID_TOKEN";
	private static final String PREF_LANGUAGE_CODE = "LANGUAGE_CODE";
	private static final String PREF_LANGUAGE_HAS_CHANGE = "LANGUAGE_HAS_CHANGE";
	private static final String PREF_LOGINED = "PREF_LOGINED";
	private static final String PREF_MASTER_CONFIG_FILE_ID = "MASTER_CONFIG_FILE_ID";
	private static final String PREF_OFFLINE_MODE = "PREF_OFFLINE_MODE";
	private static final String PREF_REGISTRATION_ID = "PREF_REGISTRATION_ID";
	private static final String PREF_SETUP_ACCOUNT_COMPLETE = "PREF_SETUP_ACCOUNT_COMPLETE";
	private static final String PREF_START_SYNC = "PREF_START_SYNC";
	private static final String PREF_TOKEN = "PREF_TOKEN";
	private static final String PREF_USER_LOGIN = "USER_LOGIN";
	private static GenericSharePreference sInstance = null;

	public static GenericSharePreference getInstance() {
		if (sInstance == null) {
			synchronized (GenericSharePreference.class) {
				if (GenericApplication.getInstance() != null) {
					sInstance = new GenericSharePreference();
					sInstance.mContext = GenericApplication.getInstance()
					        .getApplicationContext();
				}
			}
		}
		return sInstance;
	}

	public static GenericSharePreference getInstance(final Context context) {
		if (sInstance == null) {
			synchronized (GenericSharePreference.class) {
				if (context != null) {
					sInstance = new GenericSharePreference();
					sInstance.mContext = context;
				}
			}
		}
		return sInstance;
	}

	private Context mContext;

	public boolean clearSharePreference() {
		final MasterConfig mMasterConfig = MasterConfigManager.getInstance()
		        .get(GenericSharePreference.getInstance()
		                .getMasterConfigFileID());
		final String registrationID = getRegistrationId();
		int masterConfigFileID = getMasterConfigFileID();
		final boolean result = PreferenceManager
		        .getDefaultSharedPreferences(this.mContext).edit().clear()
		        .commit();
		setRegistrationId(registrationID);
		setMasterConfigFileID(masterConfigFileID);
		setOfflineMode(mMasterConfig.isHasOfflineMode());
		if (mMasterConfig.getAutoSync() != null) {
			setEnableAutoSync(mMasterConfig.getAutoSync().isEnable());
		}
		if (mMasterConfig.getCloudNotification() != null) {
			setHasCloudMessage(true);
		}

		return result;
	}

	public String getApplicationName() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getString(PREF_APPLICATION_NAME, null);
	}

	public int getAppVersion() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getInt(PREF_APP_VERSION, -1);
	}

	public String getCurrentLanguage() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getString(PREF_CURRENT_LANGUAGE, Language.DEFAULT_LANGUAGE);
	}

	public String getLanguageCode() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getString(PREF_LANGUAGE_CODE, Language.DEFAULT_LANGUAGE_CODE);
	}

	public int getMasterConfigFileID() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getInt(PREF_MASTER_CONFIG_FILE_ID, 0);
	}

	public String getRegistrationId() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getString(PREF_REGISTRATION_ID, null);
	}

	public String getToken() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getString(PREF_TOKEN, null);
	}

	public String getUserlogin() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getString(PREF_USER_LOGIN, null);
	}

	public String getValue(final SharePreferenceType sharePreferenceType) {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getString(sharePreferenceType.getValue(), null);
	}

	public boolean hasCloudMessage() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_CLOUD_MESSAGE, false);
	}

	public boolean isAutoSyncPending() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_AUTO_SYNC_PENDING, false);
	}

	public boolean isDatabaseModified() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_DATABASE_MODIFIED, false);
	}

	public boolean isEnableAutoSync() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_ENABLE_AUTO_SYNC, false);
	}

	public boolean isFirstStart() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_FIRST_START, true);
	}

	public boolean isInvalidToken() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_INVALID_TOKEN, false);
	}

	public boolean isLanguageChanged() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_LANGUAGE_HAS_CHANGE, false);
	}

	public boolean isLogined() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_LOGINED, false);
	}

	public boolean isOfflineMode() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_OFFLINE_MODE, false);
	}

	public boolean isSetUpAccountComplete() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_SETUP_ACCOUNT_COMPLETE, false);
	}

	public boolean isStartAutoSync() {
		return PreferenceManager.getDefaultSharedPreferences(this.mContext)
		        .getBoolean(PREF_START_SYNC, false);
	}

	public GenericSharePreference setApplicationName(String applicationName) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putString(PREF_APPLICATION_NAME, applicationName).commit();
		return this;
	}

	public GenericSharePreference setAppVersion(final int appVersion) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putInt(PREF_APP_VERSION, appVersion).commit();
		return this;
	}

	public GenericSharePreference setAutoSyncPending(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_AUTO_SYNC_PENDING, result).commit();
		return this;
	}

	public GenericSharePreference setCurrentLanguage(String languageID,
	        String languageCode) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putString(PREF_CURRENT_LANGUAGE, languageID)
		        .putString(PREF_LANGUAGE_CODE, languageCode).commit();
		return this;
	}

	public GenericSharePreference setDatabaseModified(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_DATABASE_MODIFIED, result).commit();
		return this;
	}

	public GenericSharePreference setEnableAutoSync(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_ENABLE_AUTO_SYNC, result).commit();
		return this;
	}

	public GenericSharePreference setFirstStart(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_FIRST_START, result).commit();
		return this;
	}

	public GenericSharePreference setHasCloudMessage(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_CLOUD_MESSAGE, result).commit();
		return this;
	}

	public GenericSharePreference setInvalidToken(boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_INVALID_TOKEN, result).commit();
		return this;
	}

	public GenericSharePreference setLanguageChanged(boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_LANGUAGE_HAS_CHANGE, result).commit();
		return this;
	}

	public GenericSharePreference setLogined(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_LOGINED, result).commit();
		return this;
	}

	public GenericSharePreference setMasterConfigFileID(int fileID) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putInt(PREF_MASTER_CONFIG_FILE_ID, fileID).commit();
		return this;
	}

	public GenericSharePreference setOfflineMode(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_OFFLINE_MODE, result).commit();
		return this;
	}

	public GenericSharePreference setRegistrationId(final String registrationId) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putString(PREF_REGISTRATION_ID, registrationId).commit();
		return this;
	}

	public GenericSharePreference setStartAutoSync(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_START_SYNC, result).commit();
		return this;
	}

	public GenericSharePreference setToken(final String registrationId) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putString(PREF_TOKEN, registrationId).commit();
		return this;
	}

	public GenericSharePreference setUpAccountComplete(final boolean result) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putBoolean(PREF_SETUP_ACCOUNT_COMPLETE, result).commit();
		return this;
	}

	public GenericSharePreference setUserlogin(final String registrationId) {
		PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
		        .putString(PREF_USER_LOGIN, registrationId).commit();
		return this;
	}
}
